<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Redis\Channel;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 小林
 */
class XiaolinController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.ordNo") ? I("request.ordNo") : I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        //$isMobile = isMobile();
        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                $pay_bankcode = 'ZFBH5';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $data = array(
            'merCode' => $this->config['mch_id'],// 商户号
            'tranNo' => $return['orderid'],//订单号
            'tranType' => '03',//扫码(电脑端)：00，网银(电脑端): 01，快捷(电脑端/手机端)：02,H5(手机端)：03
            'collectWay' => $pay_bankcode,//微信扫码(WXZF)、支付宝扫码(ZFBZF)、QQ扫码(QQZF)、银联扫码(UPZF)、京东扫码(JDZF)、微信H5(WXH5)、支付宝H5(ZFBH5)、QQH5(QQZF5)、京东H5(JDH5)、快捷H5(QUICKH5)、银联H5(UPH5)、网银(WEB)
            'tranTime' => date('YmdHis'), //交易时间，格式为yyyyMMddHHmmss
            'tranAmt' => strval($return['amount']*100),// 交易金额，单位：分
            'orderDesc' => 'wlf',  //订单描述
            "noticeUrl"=>$return['notifyurl']
        );

        $md5str = "collectWay={$data['collectWay']}&merCode={$data['merCode']}&noticeUrl={$data['noticeUrl']}&orderDesc={$data['orderDesc']}&tranAmt={$data['tranAmt']}&tranNo={$data['tranNo']}&tranTime={$data['tranTime']}&tranType={$data['tranType']}{$this->config['signkey']}";
        $data['sign'] = md5($md5str);

        po_get($this->config['gateway'], $data);
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();

        //IP白名单
        if(!$this->checkip(['39.100.102.181'])){
            exit("wrong");
        }

        $p = json_decode(file_get_contents('php://input'),true);

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['tranNo'];

        //获取订单
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据

        if($p['status'] === '0'){
            M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

                "pay_orderid_rd"=>0,
                "pay_true"=>$p['tranAmt'],
            ));
        }else{
            M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

                "pay_orderid_rd"=>0,
            ));
        }

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        unset($p['sign']);

        ksort($p);
        $md5str = httpstr($p).$this->config['signkey'];
        $sign2  = md5($md5str);

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        if($p['status'] !== '0'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],"000000");
    }

    public function callBackUrl(){
        echo '处理成功'; exit;
    }
}